package com.jxinternet.platform.services.service.impl;

import com.jxinternet.platform.services.entity.domian.Fish;
import com.jxinternet.platform.services.entity.domian.Message;
import com.jxinternet.platform.services.entity.domian.User;
import com.jxinternet.platform.services.entity.domian.Wealth;
import com.jxinternet.platform.services.entity.protobuf.BattleProtos.BattleRequest;
import com.jxinternet.platform.services.entity.protobuf.BattleProtos.BattleRequest.FishRequest;
import com.jxinternet.platform.services.entity.protobuf.BattleProtos.BattleResponse;
import com.jxinternet.platform.services.entity.protobuf.BattleProtos.BattleResponse.FishResponse;
import com.jxinternet.platform.services.protocol.BaseProtocol;
import com.jxinternet.platform.services.service.GameService;
import com.jxinternet.platform.services.service.impl.steps.BattleSteps;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * Created by gold-jx on 2017/7/19.
 */
@Slf4j
@Component
public class GameServiceImpl implements GameService {

    @Autowired
    private BattleSteps battleSteps;

    private Message message;

    @Override
    public BattleResponse battle(BattleRequest battleRequest, String userName) {
        BattleResponse.Builder builder = BattleResponse.newBuilder();
        builder.setUserName("");
        builder.setRarningsPoint(0);
        //log.info("开始作战："+"battleRequest=["+battleRequest+"],userName=["+userName+"]");

        try {

            //1.验证请求
            message=battleSteps.step1_validateRequest(battleRequest,userName);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            //2.获取用户
            message = battleSteps.step2_validateUser(userName);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            User user= (User) message.getData();

            //3.获取财产
            message=battleSteps.step3_validateWealth(userName,battleRequest.getPoint());
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }
            Wealth wealth= (Wealth) message.getData();


            long request_point = battleRequest.getPoint();//请求积分
            long rarningsPoint = 0;//盈利积分
            List<FishRequest> fishRequestList = battleRequest.getFishRequestList();
            List<FishResponse> fishResponseList = new ArrayList<>();
            for (FishRequest fishRequest : fishRequestList) {
                //4 获取鱼
                message=battleSteps.step4_validateFish(fishRequest.getFishCode());
                Fish fish = (Fish) message.getData();
                if (message != null && message.getCode() != null) {
                    builder.setResultStatus(message.getCode());
                    builder.setResultDescription(message.getRes());
                    return builder.build();
                }

                //5.是否被击中
                message = battleSteps.step5_isHit(fish);
                if (message != null && message.getCode() != null) {
                    builder.setResultStatus(message.getCode());
                    builder.setResultDescription(message.getRes());
                    return builder.build();
                }
                boolean flag= (boolean) message.getData();

                //6.计算积分

                message=battleSteps.step6_reckonPoints(wealth, battleRequest.getPoint(), fish, flag);
                if (message != null && message.getCode() != null) {
                    builder.setResultStatus(message.getCode());
                    builder.setResultDescription(message.getRes());
                    return builder.build();
                }
                Integer fishPoint = (Integer) message.getData();

                //7.添加鱼响应
                FishResponse fishResponse = FishResponse.newBuilder()
                        .setFishId(fishRequest.getFishId())
                        .setFishCode(fishRequest.getFishCode())
                        .setIsHit(flag)
                        .setFishPoint(fishPoint)
                        .build();

                //8.运算盈利积分
                rarningsPoint = rarningsPoint + fishPoint;
                fishResponseList.add(fishResponse);
            }

            //9.更新财富积分
            long endPoint = wealth.getRechargePoint() - battleRequest.getPoint() + rarningsPoint;
            message=battleSteps.step7_updatePoint(wealth, endPoint);
            if (message != null && message.getCode() != null) {
                builder.setResultStatus(message.getCode());
                builder.setResultDescription(message.getRes());
                return builder.build();
            }

            //10.添加作战响应
            builder.setUserName(userName);
            builder.setRarningsPoint(rarningsPoint);
            builder.setResultStatus(BaseProtocol.Data_Succeed_Code);
            builder.setResultDescription(BaseProtocol.Data_Succeed_Des);
            for (int i = 0; i < fishResponseList.size(); i++) {
                builder.setFishResponse(i, fishResponseList.get(i));
            }
            //log.info("结束作战");
            return builder.build();

        } catch (Exception e) {
            builder.setResultStatus(BaseProtocol.Battle_Invalid_Exception_Code);
            builder.setResultDescription(BaseProtocol.Battle_Invalid_Exception_Res);
            //log.error("作战失败",e);
            return builder.build();

        }
    }
}
